﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAL
{
    public class RecordDAL:Base<Record>
    {
        public RecordDAL() : base("record", "RID") { }
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="entity">记录实体【读写】</param>
        /// <returns></returns>
        public override bool Insert(Record entity)
        {
            string sql = "insert into `record` (`AID`,`Money`,`Create_Time`,`Category_ID`,`Remark`,`Card_ID`) values(@aid,@money,@time,@cid,@remark,@cardId)";
            var r = MySQLHelper.ExecuteNonQuery(sql,
                    GetParams("aid", entity.AccountID),
                    GetParams("money", entity.Money),
                    GetParams("time", entity.CreateTime),
                    GetParams("cid", entity.CategoryID),
                    GetParams("remark", entity.Remark),
                    GetParams("cardId", entity.CardID)
                );
            if (r == 1)
            {
                entity.ID = GetLastInsertID();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 自动记账
        /// </summary>
        /// <param name="auto"></param>
        public void AutoInsert(AutoSetting auto)
        {
            string sql = "call `sp_autoInsertRecord`(@aid,@money,@card,@cid,@r,@time,@type);";
            MySQLHelper.ExecuteNonQuery(sql, GetParams("aid", auto.AID), GetParams("money", auto.Model.Money),
                GetParams("card", auto.Model.CardID), GetParams("@cid", auto.Model.CategoryID),
                GetParams("r", auto.Model.Remark), GetParams("time", auto.LastTime), GetParams("type", auto.AutoType));
        }

        public override bool Update(Record entity)
        {
            throw new NotImplementedException();
        }

        public override Record GetEntityById(int id)
        {
            throw new NotImplementedException();
        }

        public override Record GetEntityById(string id)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 查询用户的所有记录
        /// </summary>
        /// <param name="aid">用户账号ID</param>
        /// <returns></returns>
        public IEnumerable<Record> QueryMyList(int aid)
        {
            string sql = "select * from record where aid = @aid";
            DataTable table = MySQLHelper.Query(sql, GetParams("aid", aid));
            List<Record> list = new List<Record>();
            foreach (DataRow row in table.Rows)
            {
                Record r = new Record()
                {
                    AccountID = aid,
                    CategoryID = int.Parse(row["Category_ID"].ToString()),
                    ID = int.Parse(row["RID"].ToString()),
                    Remark = row["remark"].ToString(),
                    CreateTime = DateTime.Parse(row["Create_Time"].ToString()),
                    Money = decimal.Parse(row["money"].ToString()),
                    CardID = int.Parse(row["card_id"].ToString())
                };
                list.Add(r);
            }
            return list;

        }
    }
}
